//
//  FGVoiceMessageCell.m
//  FGChatKit
//
//  Created by Henry on 2021/6/3.
//

#import "FGVoiceMessageCell.h"
#import "UIView+FGLayout.h"

@implementation FGVoiceMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _voiceView = [[UIImageView alloc] init];
        _voiceView.animationDuration = 1;
        [self.bubbleView addSubview:_voiceView];

        _durationLabel = [[UILabel alloc] init];
        _durationLabel.font = [UIFont systemFontOfSize:12];
        _durationLabel.textColor = [UIColor blackColor];
        [self.bubbleView addSubview:_durationLabel];

        _voiceReadPointView = [[UIImageView alloc] init];
        _voiceReadPointView.backgroundColor = [UIColor redColor];
        _voiceReadPointView.frame = CGRectMake(0, 0, 8, 8);
        _voiceReadPointView.hidden = YES;
        _voiceReadPointView.layer.cornerRadius = _voiceReadPointView.frame.size.width/2;
        _voiceReadPointView.layer.masksToBounds = YES;
        [self.bubbleView addSubview:_voiceReadPointView];
    }
    return self;
}

- (void)fillWithData:(FGVoiceMessageCellData *)data {
    [super fillWithData:data];
    self.voiceData = data;
    
    _durationLabel.textColor = data.textColor;
    _durationLabel.font = data.textFont;

    
    if (data.duration > 0) {
        _durationLabel.text = [NSString stringWithFormat:@"%ld\"", (long)data.duration];
    } else {
        _durationLabel.text = @"1\"";    // 显示0秒容易产生误解
    }
    _voiceView.image = data.voiceImage;
    _voiceView.animationImages = data.voiceAnimationImages;
    
    // voiceReadPoint
    // 未播放展示红点
    // 发送出的消息，不展示红点
    if (self.voiceData.direction == FGMsgDirectionIncoming && !self.voiceData.played) {
        self.voiceReadPointView.hidden = NO;
    }
    
    [self.voiceData addObserver:self forKeyPath:@"isPlaying" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial context:nil];
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"isPlaying"]) {
        if (self.voiceData.isPlaying) {
            [self.voiceView startAnimating];
            if (!self.voiceReadPointView.hidden) {
                self.voiceReadPointView.hidden = YES;
            }
        } else {
            [self.voiceView stopAnimating];
        }
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.durationLabel.mm_sizeToFitThan(10, 40).mm__centerY(self.bubbleView.mm_h/2);
    
    self.voiceView.mm_sizeToFit().mm_top(12);
    
    if (self.voiceData.direction == FGMsgDirectionOutgoing) {
        
        self.voiceView.mm_right(self.voiceData.cellLayout.bubbleInsets.right);
        
        self.durationLabel.mm_right(self.voiceView.mm_r + self.voiceView.mm_w + 6);

        self.voiceReadPointView.hidden = YES;
    } else {
        
        self.voiceView.mm_left(self.voiceData.cellLayout.bubbleInsets.left);
        
        self.durationLabel.mm_left(self.voiceView.mm_maxX + 6);
        
        self.voiceReadPointView.mm__centerY(self.bubbleView.mm_centerY).mm_left(self.bubbleView.mm_maxX + 10);
        
    }
}

- (void)dealloc {
    if (self.voiceData) {
        [self.voiceData removeObserver:self forKeyPath:@"isPlaying" context:nil];
    }
}

@end
